package com.atguigu.leetcode;

/**
 * 查找第二大数
 * @author xr-8
 * @date 2023-08-02
 * @time 23:13
 */
public class SecondNum {

    public static void main(String[] args) {
        int arr[] = {4, 1, 2, 3, 5, 7, 10, 9, 18, 9, 7, -1};

        System.out.println(arr[findSecondNum(arr)]);

    }

    private static int findSecondNum(int[] arr) {
        int maxIndex = 0;
        int secondIndex = -1;

        for (int i = 1; i < arr.length; i++) {
            if (arr[i] >= arr[maxIndex]) {
                secondIndex = maxIndex;
                maxIndex = i;
            } else {
                if (secondIndex == -1) {
                    secondIndex = i;
                } else if (arr[i] > arr[secondIndex]) {
                    secondIndex = i;
                }
            }
        }
        System.out.println(maxIndex + "," + secondIndex);
        return secondIndex;
    }
}
